﻿
Imports System.IO
Imports std
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl

Public Class links
    Inherits cacheProcessor

    Public Const _pathidentity As String = "/links"
    Public Const defaultImage As String = defaultLinkImage
    Public Const countquery As String = "c"
    Private WithEvents linkconfig As configMonitor = Nothing

    Public Overrides Sub initial(Optional ByVal config As configParser = Nothing)
        MyBase.initial()
        Dim linkfile As String = Nothing
        linkfile = config.parameter(configNaming.links.name, configNaming.links.linkfile _
                                    , configNaming.defaultValue.links.linkfile)
        linkconfig = New configMonitor()
        assert(linkconfig.open(linkfile), "cannot open linkfile " + linkfile)
    End Sub

    Protected Overrides Function retiredticks() As Long
        'manually retire according to configuration change
        Return -1
    End Function

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Public Overrides Function pathidentitylen() As Long
        Return strlen(pathidentity())
    End Function

    Protected Overrides Function processImpl(ByVal context As httpContext) As Boolean
        Dim links As vector(Of String) = Nothing
        Dim titles As vector(Of String) = Nothing
        Dim images As vector(Of String) = Nothing
        If linkconfig.readlist(configNaming.links.name, configNaming.links.link, links) _
            AndAlso linkconfig.readlist(configNaming.links.name, configNaming.links.title, titles) _
            AndAlso linkconfig.readlist(configNaming.links.name, configNaming.links.image, images) Then
            Dim buff As MemoryStream = Nothing
            buff = context.buff()

            Dim page As xhtmlpage = Nothing
            page = New xhtmlpage(buff)
            page.title().text = applicationSign() + " - links"
            page.body().id = "id_links"

            Dim csslink As csslink = Nothing
            csslink = New csslink(buff)
            csslink.href = csslinkhref
            page.head().addchild(csslink)

            Dim label As label = Nothing
            label = New label(buff)
            label.id = "id_label"
            label.text = "following links"
            page.body().addchild(label)

            Dim linkcountStr As String = Nothing
            linkcountStr = context.url.query(countquery)
            Dim linkcount As Int64
            If Not isemptystring(linkcountStr) Then
                If Not convertor.toint64(linkcountStr, linkcount) Then
                    raiseError("cannot convert count " + linkcountStr + " in url to int64, use default value." _
                               , errorHandle.errorType.user)
                    linkcount = 0
                End If
            Else
                linkcount = 0
            End If

            Dim linkset As linkset = Nothing
            linkset = New linkset(buff)
            linkset.setdata(links, titles, images, defaultImage, , linkcount)
            page.body().addchild(linkset)

            If linkcount > 0 Then
                Dim a As a = Nothing
                a = New a(buff)
                a.href = strmid(_pathidentity, strlen(network.pathSeperator))
                a.text = "see all links"
                a.target = "_blank"
                page.body().addchild(a)
            End If

            If page.render() Then
                Return True
            Else
                raiseError("cannot render xhtmlpage, ignore request.", errorHandle.errorType.warning)
                Return False
            End If
        Else
            raiseError("cannot read link list from linkconfig, ignore request.", errorHandle.errorType.warning)
            Return False
        End If
    End Function

    Private Sub linkconfig_ConfigChanged() Handles linkconfig.ConfigChanged
        cache.clear()
    End Sub
End Class
